<?php
/**
 * Dcolors,The Community Application for Web
 *
 * Lunar class
 *
 * Not free software
 * @author     chopin xiao <x@sunsne.com>
 * @copyright  2009 The Dcolors Group
 * @version    SVN: $Id: Lunar.php 6 2009-01-11 04:10:08Z chopins.xiao $
 **/

class Lunar
{
	public $Heavenly = array("甲","乙","丙","丁","戊","己","庚","辛","壬","癸");
	public $EarthlyBranches = array("子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥");
	public $LunarNewYear = array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪");
	const YEAR = 31556900.16;
	const MONTH = 2551443.84;
	const UNIX = 45;
	const MD = 1124;
	const START = 1924;
	public function __construct()
	{
		
	}
	public function getYear($year)
	{
		if($year == 1970)
		{
			$index = self::UNIX;
		}else{
			$index = $this->getHEIndex($year);
		}
		return $this->Heavenly[$index %10].$this->EarthlyBranches[$index%12];
	}
	public function getMonth($ym=null)
	{
		$time = strtotime($ym);
		if($time === false) return false;
		return $time/self::MONTH%12;
	}
	public function isLeapYear($year)
	{
		$index = abs($year -1982)%19;
		if($index%19 == 0)
				return true;
		else
			return false;
	}
	public function getHEIndex($year)
	{
		$index = $year - 1970;
		if($index >= -46 && $index <= 13)
		{
			return self::UNIX + $index;
		}elseif($index<0){
			$index = $index - 46;
		}elseif($index > 0){
			$index = $index - 13;
		}
		return $index%60;
	}
	public function HETable()
	{
		$table = array();
		for($i=0;$i<60;$i++)
		{
			$hi = $i>=10 ? $i%10 :$i;
			$ei = $i>=12 ? $i%12 : $i;
			$table[$i] = $this->Heavenly[$hi].$this->EarthlyBranches[$ei];
		}
		return $table;
	}
}